package com.yanqu.road.dao.impl.wingroom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.wingroom.UserPatronsVisitData;
import com.yanqu.road.entity.wingroom.WingRoomVisitPatrons;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserPatronsVisitDataDaoImpl extends TempDataObjectDao<UserPatronsVisitData> {

    public static String getUserPatronsVisitDataSql = "select * from `t_u_wingroom_patrons_visit` where `userId`=?";
    public UserPatronsVisitData getUserPatronsVisitData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserPatronsVisitDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserPatronsVisitDataSql = "insert into `t_u_wingroom_patrons_visit`(userId,state,visitor,userBaseInfo,eventTime,receiveTime,doneTime,patronsList,todayTouch,resetTime) values (?,?,?,?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(UserPatronsVisitData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVisitor()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReceiveTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDoneTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPatronsList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTodayTouch()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        if (getDb().executeNoneQuery(insertUserPatronsVisitDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserPatronsVisitDataSql = "update `t_u_wingroom_patrons_visit` set state=?,visitor=?,userBaseInfo=?,eventTime=?,receiveTime=?,doneTime=?,patronsList=?,todayTouch=?,resetTime=? where userId=?";
    
    @Override
    protected boolean updateData(UserPatronsVisitData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVisitor()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReceiveTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDoneTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPatronsList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTodayTouch()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserPatronsVisitDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserPatronsVisitData parseData(ResultSet rs) throws SQLException {
        UserPatronsVisitData data = new UserPatronsVisitData();
        data.setUserId(rs.getLong("userId"));
        data.setState(rs.getInt("state"));
        data.setVisitor(rs.getLong("visitor"));
        String baseInfo = rs.getString("userBaseInfo");
        if(StringUtils.isNullOrEmpty(baseInfo)){

        }else {
            data.setUserBaseInfo(JSON.parseObject(baseInfo, UserBaseInfo.class));
        }
        data.setReceiveTime(rs.getInt("receiveTime"));
        data.setEventTime(rs.getInt("eventTime"));
        data.setDoneTime(rs.getInt("doneTime"));
        data.setTodayTouch(rs.getBoolean("todayTouch"));
        data.setResetTime(rs.getInt("resetTime"));
        data.setPatronsList(JSON.parseObject(rs.getString("patronsList"), new TypeReference<List<WingRoomVisitPatrons>>() {}));

        return data;
    }
}
